package com.manlost.web.controller.register;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.manlost.biz.ManlostServiceLocator;
import com.manlost.biz.account.AccountService;
import com.manlost.biz.account.model.constants.AccountLevel;
import com.manlost.biz.account.model.constants.AccountStatus;
import com.manlost.biz.account.model.dataobject.Account;
import com.manlost.web.common.Result;
import com.manlost.web.common.ResultBuilder;
import com.manlost.web.common.cookie.ManLostCookieSystemUtils;

/**
 * 标准注册(welcome to manlost world!)
 * 
 * @author zhoukang
 */
@Controller
public class StandardRegisterAction {
	private AccountService accountService = ManlostServiceLocator.getAccountService();

	private static final String REGISTER_REQUEST_PARAM_EMAIL 			= "email";
	private static final String REGISTER_REQUEST_PARAM_USERNAME		    = "username";
	private static final String REGISTER_REQUEST_PARAM_PASSWORD 		= "password";
	private static final String REGISTER_REQUEST_PARAM_PASSWORD_CONFIRM = "pwd_confirm";
	private static final int    PASSWORD_LIMIT_LENGTH				    = 6;

	/**
	 * 注册页面注册
	 */
	@RequestMapping(value = "/register_action.htm", method = {RequestMethod.POST, RequestMethod.GET})
	ModelAndView doRegisterCore(HttpServletRequest request, HttpServletResponse response) {
		if(!processRegister(request, response).isSuccessFlag()) {
			return null;
		}
		
		// 重定向需要重写 TODO
		String returnUrl = "http://localhost:9090/manlost/index.htm";
		return new ModelAndView("redirect:" + returnUrl);
	}

	/**
	 * 弹出框注册
	 */
	@RequestMapping(value = "/register_ajax.htm", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public Map<String, Object> ajaxLoginCore(HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("result",processRegister(request,response));
		return result;
	}
	
	
	private Result processRegister(HttpServletRequest request, HttpServletResponse response){
		String email = request.getParameter(REGISTER_REQUEST_PARAM_EMAIL);
		String username = request.getParameter(REGISTER_REQUEST_PARAM_USERNAME);
		String password = request.getParameter(REGISTER_REQUEST_PARAM_PASSWORD);
		String pwdConfirm = request.getParameter(REGISTER_REQUEST_PARAM_PASSWORD_CONFIRM);
		
		// format input
		email = StringUtils.trim(StringUtils.lowerCase(email));
		username = StringUtils.trim(username);
		password = StringUtils.trim(StringUtils.lowerCase(password));
		pwdConfirm = StringUtils.trim(StringUtils.lowerCase(pwdConfirm));
		
		// 1.校验email唯一性
		Account account = accountService.findManLostUser(email);
		if(null != account) {
			return ResultBuilder.buildResult(false, "RegisterErrorCode.EMAIL_ALREADY_REGISTERED") ;
		}
		
		// 2.校验密码
		if(StringUtils.length(password) != PASSWORD_LIMIT_LENGTH) {
			return ResultBuilder.buildResult(false, "RegisterErrorCode.PASSWORD_LENGTH_ERROR") ;
		}
		if(!StringUtils.equals(password, pwdConfirm)) {
			return ResultBuilder.buildResult(false, "RegisterErrorCode.PAWSSWORD_NOT_SAME") ;
		}
		
		// 3.注册会员
		account = buildManLostUser(email, username, password);
		account = accountService.createManLostUser(account);
		if(null == account) {
			return ResultBuilder.buildResult(false, "RegisterErrorCode.FAIL_REGISTER_ACCOUNT") ;
		}
		
		// 4.发邮件
		//MLostMailClient.asynSend(mlostMail);
	
		// 5.本地写cookie
		boolean isAutoSign = false;
		ManLostCookieSystemUtils.setManLostCookieForLogin(request, account, isAutoSign);
		
		return ResultBuilder.buildResult(true) ;
	}
	
	/**
	 * 构建注册对象
	 * 
	 * @param email		
	 * @param username
	 * @param password
	 * @return
	 */
	private Account buildManLostUser(String email, String username,
			String password) {
		
		Account account = new Account();
		account.setEmail(email);
		account.setPassword(password);
		account.setUsername(username);
		account.setStatus(AccountStatus.ENABLE);
		account.setLevel(AccountLevel.COMMON);
		
		return account;
	}
}
